package org.example.service.Impl;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.example.commonapi.domain.Course;
import org.example.commonapi.domain.ExcelListener;
import org.example.commonapi.domain.ExcelUser;
import org.example.commonapi.domain.Practice;
import org.example.commonapi.domain.R;
import org.example.commonapi.domain.SchoolAccount;
import org.example.commonapi.domain.Suggestion;
import org.example.commonapi.domain.User;
import org.example.commonapi.domain.Video;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.mapper.CourseMapper;
import org.example.commonapi.mapper.PracticeMapper;
import org.example.commonapi.mapper.SchoolAccountMapper;
import org.example.commonapi.mapper.SchoolMapper;
import org.example.commonapi.mapper.SuggestionMapper;
import org.example.commonapi.mapper.UserMapper;
import org.example.commonapi.mapper.VideoMapper;
import org.example.commonapi.util.JwtUtil;
import org.example.commonapi.util.redis.RedisService;
import org.example.commonapi.util.redis.StatisticPrefix;
import org.example.commonapi.vo.CourseVO;
import org.example.commonapi.vo.StatisticVO;
import org.example.commonapi.vo.VideoVO;
import org.example.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private JwtUtil jwtUtil;

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private SchoolAccountMapper schoolAccountMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private PracticeMapper practiceMapper;

    @Resource
    private RedisService redisService;

    @Autowired
    private SuggestionMapper suggestionMapper;

    @Override
    public User login(String username, String password) {
        String passwordMd5= DigestUtils.md5Hex(password);
        //利用用户名进行搜索
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username).eq(User::getPassword, passwordMd5);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            // 更新用户登陆时间
            user.setUpdateTime(LocalDateTime.now());
            userMapper.updateById(user);
        }
        return user;
    }

    @Override
    public Long register(LoginInfo loginInfo) {
        //加密
        String passwordMd5= DigestUtils.md5Hex(loginInfo.getPassword());
        loginInfo.setPassword(passwordMd5);
        User user=new User(loginInfo);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setRole(4);
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public R<User> getNowUserInfo(HttpServletRequest request) {
        Long userId = jwtUtil.parseJwt(request.getHeader("token")).getId();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, userId);
        User user = userMapper.selectOne(queryWrapper);
        return R.success(user);
    }

    @Override
    public R<List<User>> schoolGetUserInfo(HttpServletRequest request, Integer role) {
        Long schoolUserId = jwtUtil.parseJwt(request.getHeader("token")).getId();
        // 先从SchoolAccount中查询本账号对应的学校id
        LambdaQueryWrapper<SchoolAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SchoolAccount::getUserId, schoolUserId);
        SchoolAccount schoolAccount = schoolAccountMapper.selectOne(queryWrapper);
        // 再根据schoolId去查询所有相关的学生信息
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getSchoolId, schoolAccount.getSchoolId());
        lambdaQueryWrapper.eq(User::getRole, role);
        List<User> users = userMapper.selectList(lambdaQueryWrapper);
        return R.success(users);
    }

    @Override
    public R<List<CourseVO>> schoolGetCourseInfo(HttpServletRequest request) {
        Long schoolUserId = jwtUtil.parseJwt(request.getHeader("token")).getId();
        // 先从SchoolAccount中查询本账号对应的学校id
        LambdaQueryWrapper<SchoolAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SchoolAccount::getUserId, schoolUserId);
        SchoolAccount schoolAccount = schoolAccountMapper.selectOne(queryWrapper);
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Course::getSchoolId, schoolAccount.getSchoolId());
        List<Course> courses = courseMapper.selectList(lambdaQueryWrapper);
        List<CourseVO> courseVOS = new ArrayList<>();
        courses.forEach(course -> {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getId, course.getTeacherId());
            User user = userMapper.selectOne(userLambdaQueryWrapper);
            CourseVO courseVO = new CourseVO();
            BeanUtils.copyProperties(course, courseVO);
            courseVO.setTeacherName(user.getUsername());
            courseVOS.add(courseVO);
        });
        return R.success(courseVOS);
    }

    @Override
    public R<List<VideoVO>> schoolGetVideoInfo(HttpServletRequest request) {
        Long schoolUserId = jwtUtil.parseJwt(request.getHeader("token")).getId();
        // 先从SchoolAccount中查询本账号对应的学校id
        LambdaQueryWrapper<SchoolAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SchoolAccount::getUserId, schoolUserId);
        SchoolAccount schoolAccount = schoolAccountMapper.selectOne(queryWrapper);
        // 先获取所有视频
        List<Video> videos = videoMapper.selectList(null);
        List<VideoVO> videoVOList = new ArrayList<>();
        // 获取每个视频发布者 根据发布者id查询学校id
        videos.forEach(video -> {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getId, video.getTeacherId());
            User user = userMapper.selectOne(userLambdaQueryWrapper);
            if (user.getSchoolId().equals(schoolAccount.getSchoolId())) {
                VideoVO videoVO = new VideoVO();
                BeanUtils.copyProperties(video, videoVO);
                videoVO.setPublisherName(user.getUsername());
                videoVOList.add(videoVO);
            }
        });
        return R.success(videoVOList);
    }

    @Override
    public R<List<CourseVO>> adminGetCourseInfo() {
        List<Course> courses = courseMapper.selectList(null);
        List<CourseVO> courseVOS = new ArrayList<>();
        courses.forEach(course -> {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getId, course.getTeacherId());
            User user = userMapper.selectOne(userLambdaQueryWrapper);
            CourseVO courseVO = new CourseVO();
            BeanUtils.copyProperties(course, courseVO);
            courseVO.setTeacherName(user.getUsername());
            courseVOS.add(courseVO);
        });
        return R.success(courseVOS);
    }

    @Override
    public R<List<VideoVO>> adminGetVideoInfo() {
        List<Video> videos = videoMapper.selectList(null);
        List<VideoVO> videoVOList = new ArrayList<>();
        videos.forEach(video -> {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getId, video.getTeacherId());
            User user = userMapper.selectOne(userLambdaQueryWrapper);
            VideoVO videoVO = new VideoVO();
            BeanUtils.copyProperties(video, videoVO);
            videoVO.setPublisherName(user.getUsername());
            videoVOList.add(videoVO);
        });
        return R.success(videoVOList);
    }

    @Override
    public R<List<Video>> getMaxPlayCountVideo(HttpServletRequest request,Integer role) {
        Long schoolId;
        if ( role == 4 ) {
            Long schoolUserId = jwtUtil.parseJwt(request.getHeader("token")).getId();
            // 先从SchoolAccount中查询本账号对应的学校id
            LambdaQueryWrapper<SchoolAccount> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SchoolAccount::getUserId, schoolUserId);
            SchoolAccount schoolAccount = schoolAccountMapper.selectOne(queryWrapper);
            schoolId = schoolAccount.getSchoolId();
        } else {
            schoolId = null;
        }
        // 获取每个视频发布者 根据发布者id查询学校id
        List<Video> videos = videoMapper.selectList(null);
        List<Video> resultVideoList = new ArrayList<>();
        videos.forEach(video -> {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getId, video.getTeacherId());
            User user = userMapper.selectOne(userLambdaQueryWrapper);
            if ( role == 4 && user.getSchoolId().equals(schoolId) ) {
                resultVideoList.add(video);
            }
            if ( role == 3 ) {
                resultVideoList.add(video);
            }
        });
        // 获取访问量排行前8的视频
        resultVideoList.sort(Comparator.comparing(Video::getPlayCount).reversed());
        List<Video> videoList = resultVideoList.subList(0, Math.min(8, resultVideoList.size()));
        return R.success(videoList);
    }

    @Override
    public R<List<Practice>> getMaxVisitPractice(HttpServletRequest request, Integer role) {
        Long schoolId = null;
        if ( role == 4 ) {
            // 获取本校发布的练习中访问量排行前8的题目
            Long schoolUserId = jwtUtil.parseJwt(request.getHeader("token")).getId();
            // 获取本账号对应的学校id
            LambdaQueryWrapper<SchoolAccount> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SchoolAccount::getUserId, schoolUserId);
            SchoolAccount schoolAccount = schoolAccountMapper.selectOne(queryWrapper);
            schoolId = schoolAccount.getSchoolId();
        }
        // 获取每个练习发布者 根据发布者id查询学校id
        List<Practice> practices = practiceMapper.selectList(null);
        List<Practice> schoolPractices = new ArrayList<>();
        if ( role == 4 ) {
            for (Practice practice : practices) {
                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
                userLambdaQueryWrapper.eq(User::getId, practice.getTeacherId());
                User user = userMapper.selectOne(userLambdaQueryWrapper);
                if (user.getSchoolId().equals(schoolId))
                    schoolPractices.add(practice);
            }
        }else {
            schoolPractices = practices;
        }
        // 获取访问量排行前8的题目
        schoolPractices.sort(Comparator.comparing(Practice::getVisits).reversed());
        List<Practice> practiceList = schoolPractices.subList(0, Math.min(8, practices.size()));
        return R.success(practiceList);
    }

    @Override
    public R<List<Integer>> getTodayUserInfo(HttpServletRequest request,Integer role) {
        Long schoolId = null;
        if ( role == 4 ) {
            Long schoolUserId = jwtUtil.parseJwt(request.getHeader("token")).getId();
            // 获取本账号对应的学校id
            LambdaQueryWrapper<SchoolAccount> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SchoolAccount::getUserId, schoolUserId);
            SchoolAccount schoolAccount = schoolAccountMapper.selectOne(queryWrapper);
            schoolId = schoolAccount.getSchoolId();
        }
        // 将今天按每隔3小时分割并获取3小时内的用户活跃度
        List<Integer> todayUsers = new ArrayList<>();
        for (int i = 0; i < 24; i += 3) {
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.between(User::getCreateTime, LocalDateTime.now().minusHours(i - 3), LocalDateTime.now().minusHours(i));
            if ( role == 4 ) {
                lambdaQueryWrapper.eq(User::getSchoolId, schoolId);
            }
            todayUsers.add(Math.toIntExact(userMapper.selectCount(lambdaQueryWrapper)));
        }
        return R.success(todayUsers);
    }

    @Override
    public R<List<StatisticVO>> getStatisticData(HttpServletRequest request, Integer role) {
        Long schoolId = null;
        if ( role == 4 ) {
            Long schoolUserId = jwtUtil.parseJwt(request.getHeader("token")).getId();
            // 获取本账号对应的学校id
            LambdaQueryWrapper<SchoolAccount> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SchoolAccount::getUserId, schoolUserId);
            SchoolAccount schoolAccount = schoolAccountMapper.selectOne(queryWrapper);
            schoolId = schoolAccount.getSchoolId();
        }
        List<StatisticVO> statisticVOList = new ArrayList<>();
        // 获取现在所有用户数量
        LambdaQueryWrapper<User> userCountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if ( role == 4 ) {
            userCountLambdaQueryWrapper.eq(User::getSchoolId, schoolId);
        }
        int userCount = Math.toIntExact(userMapper.selectCount(userCountLambdaQueryWrapper));
        // 获取昨日日期
        String yesterday = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        if ( redisService.get(StatisticPrefix.USER_COUNT_ADMIN,yesterday) == null ) {
            redisService.set(StatisticPrefix.USER_COUNT_ADMIN,yesterday,20,86400);
        }
        if ( schoolId != null && redisService.get(StatisticPrefix.USER_COUNT,schoolId + ":" + yesterday) == null) {
            redisService.set(StatisticPrefix.USER_COUNT, schoolId + ":" + yesterday, 20, 86400);
        }
        int yesterdayUserCount;
        if ( role == 3 ) {
            yesterdayUserCount = Integer.parseInt(redisService.get(StatisticPrefix.USER_COUNT_ADMIN,yesterday).toString());
        } else {
            yesterdayUserCount = Integer.parseInt(redisService.get(StatisticPrefix.USER_COUNT,schoolId + ":" + yesterday).toString());
        }
        System.out.println("昨日用户总量：" + yesterdayUserCount);
        // 计算用户增长量
        double userGrowth = (userCount - yesterdayUserCount) * 1.0 / yesterdayUserCount * 100;
        statisticVOList.add(new StatisticVO("今日用户总数",userCount, userGrowth >= 0 ? 1 : 0, userGrowth));
        // 获取今日用户活跃度
        LambdaQueryWrapper<User> todayUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if ( role == 4 ) {
            todayUserLambdaQueryWrapper.eq(User::getSchoolId, schoolId);
        }
        todayUserLambdaQueryWrapper.between(User::getUpdateTime, LocalDateTime.now().minusDays(1), LocalDateTime.now());
        int todayUserCount = Math.toIntExact(userMapper.selectCount(todayUserLambdaQueryWrapper));
        if ( redisService.get(StatisticPrefix.USER_ACTIVE_ADMIN,yesterday) == null ) {
            redisService.set(StatisticPrefix.USER_ACTIVE_ADMIN,yesterday,1,86400);
        }
        if ( schoolId != null && redisService.get(StatisticPrefix.USER_ACTIVE,schoolId + ":" + yesterday) == null) {
            redisService.set(StatisticPrefix.USER_ACTIVE, schoolId + ":" + yesterday, 1, 86400);
        }
        int yesterdayUserActiveCount;
        if ( role == 3 ) {
            yesterdayUserActiveCount = Integer.parseInt(redisService.get(StatisticPrefix.USER_ACTIVE_ADMIN,yesterday).toString());
        } else {
            yesterdayUserActiveCount = Integer.parseInt(redisService.get(StatisticPrefix.USER_ACTIVE,schoolId + ":" + yesterday).toString());
        }
        double userActive = (todayUserCount - yesterdayUserActiveCount) * 1.0 / yesterdayUserActiveCount * 100;
        statisticVOList.add(new StatisticVO("今日用户活跃数",todayUserCount, userActive >= 0 ? 1 : 0, userActive));
        // 获取今日视频总的播放量
        LambdaQueryWrapper<Video> videoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Video> videoList = videoMapper.selectList(videoLambdaQueryWrapper);
        List<Video> schoolVideoList = new ArrayList<>();
        if ( role == 4 ) {
            for (Video video : videoList) {
                // 查询出所有本学校的视频
                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
                userLambdaQueryWrapper.eq(User::getId, video.getTeacherId());
                User user = userMapper.selectOne(userLambdaQueryWrapper);
                if ( user.getSchoolId().equals(schoolId) ) {
                    schoolVideoList.add(video);
                }
            }
        }else {
            schoolVideoList = videoList;
        }
        int todayVideoPlayCount = 0;
        for (Video video : schoolVideoList) {
            if ( video.getPlayCount() != null )
                todayVideoPlayCount += video.getPlayCount();
        }
        // 获取昨日视频播放量
        if ( redisService.get(StatisticPrefix.VIDEO_PLAYCOUNT_ADMIN,yesterday) == null ) {
            redisService.set(StatisticPrefix.VIDEO_PLAYCOUNT_ADMIN,yesterday,2000,86400);
        }
        if ( schoolId!= null && redisService.get(StatisticPrefix.VIDEO_PLAYCOUNT,schoolId + ":" + yesterday) == null  )  {
            redisService.set(StatisticPrefix.VIDEO_PLAYCOUNT,schoolId + ":" + yesterday, 200, 86400);
        }
        int yesterdayVideoPlayCount;
        if ( role == 3 ) {
            yesterdayVideoPlayCount = Integer.parseInt(redisService.get(StatisticPrefix.VIDEO_PLAYCOUNT_ADMIN,yesterday).toString());
        } else {
            yesterdayVideoPlayCount = Integer.parseInt(redisService.get(StatisticPrefix.VIDEO_PLAYCOUNT,schoolId + ":" + yesterday).toString());
        }
        double videoPlayCount = (todayVideoPlayCount - yesterdayVideoPlayCount) * 1.0 / yesterdayVideoPlayCount * 100;
        statisticVOList.add(new StatisticVO("到今日视频播放总量",todayVideoPlayCount, videoPlayCount >= 0 ? 1 : 0, videoPlayCount));
        // 获取今日题目访问量
        LambdaQueryWrapper<Practice> practiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Practice> practiceList = practiceMapper.selectList(practiceLambdaQueryWrapper);
        List<Practice> schoolPracticeList = new ArrayList<>();
        if ( role == 4 ) {
            for (Practice practice : practiceList) {
                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
                userLambdaQueryWrapper.eq(User::getId, practice.getTeacherId());
                User user = userMapper.selectOne(userLambdaQueryWrapper);
                if ( user.getSchoolId().equals(schoolId) ) {
                    schoolPracticeList.add(practice);
                }
            }
        }else {
            schoolPracticeList = practiceList;
        }
        int todayPracticeCount = 0;
        for (Practice practice : schoolPracticeList) {
            if ( practice.getVisits() != null )
                todayPracticeCount += practice.getVisits();
        }
        // 获取昨日题目访问量
        if ( redisService.get(StatisticPrefix.PRACTICE_VISIT_ADMIN,yesterday) == null ) {
            redisService.set(StatisticPrefix.PRACTICE_VISIT_ADMIN,yesterday,1000,86400);
        }
        if ( schoolId != null && redisService.get(StatisticPrefix.PRACTICE_VISIT,schoolId + ":" + yesterday) == null ) {
            redisService.set(StatisticPrefix.PRACTICE_VISIT,schoolId + ":" + yesterday, 500, 86400);
        }
        int yesterdayPracticeCount;
        if ( role == 3 ) {
            yesterdayPracticeCount = Integer.parseInt(redisService.get(StatisticPrefix.PRACTICE_VISIT_ADMIN,yesterday).toString());
        } else {
            yesterdayPracticeCount = Integer.parseInt(redisService.get(StatisticPrefix.PRACTICE_VISIT,schoolId + ":" + yesterday).toString());
        }
        double practiceCount = (todayPracticeCount - yesterdayPracticeCount) * 1.0 / yesterdayPracticeCount * 100;
        statisticVOList.add(new StatisticVO("到今日题目访问总量",todayPracticeCount, practiceCount >= 0 ? 1 : 0, practiceCount));
        return R.success(statisticVOList);
    }

    @Override
    public void updateStatistic() {
        // 获取数据库中所有学校id
        List<SchoolAccount> schoolAccountList = schoolAccountMapper.selectList(null);
        // 获取当前日期
        String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        for (SchoolAccount schoolAccount : schoolAccountList) {
            Long schoolId = schoolAccount.getSchoolId();
            // 获取现在所有用户数量
            LambdaQueryWrapper<User> userCountLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userCountLambdaQueryWrapper.eq(User::getSchoolId, schoolId);
            int userCount = Math.toIntExact(userMapper.selectCount(userCountLambdaQueryWrapper));
            redisService.set(StatisticPrefix.USER_COUNT,schoolId + ":" + today,userCount,86400);
            // 获取今日用户活跃度
            LambdaQueryWrapper<User> todayUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
            todayUserLambdaQueryWrapper.eq(User::getSchoolId, schoolId);
            todayUserLambdaQueryWrapper.between(User::getUpdateTime, LocalDateTime.now().minusDays(1), LocalDateTime.now());
            int todayUserCount = Math.toIntExact(userMapper.selectCount(todayUserLambdaQueryWrapper));
            redisService.set(StatisticPrefix.USER_ACTIVE,schoolId + ":" + today,todayUserCount,86400);
            // 获取今日视频总的播放量
            LambdaQueryWrapper<Video> videoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            List<Video> videoList = videoMapper.selectList(videoLambdaQueryWrapper);
            List<Video> schoolVideoList = new ArrayList<>();
            for (Video video : videoList) {
                // 查询出所有本学校的视频
                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
                userLambdaQueryWrapper.eq(User::getId, video.getTeacherId());
                User user = userMapper.selectOne(userLambdaQueryWrapper);
                if ( user.getSchoolId().equals(schoolId) ) {
                    schoolVideoList.add(video);
                }
            }
            int todayVideoPlayCount = 0;
            for (Video video : schoolVideoList) {
                if ( video.getPlayCount() != null )
                    todayVideoPlayCount += video.getPlayCount();
            }
            redisService.set(StatisticPrefix.VIDEO_PLAYCOUNT,schoolId + ":" +today,todayVideoPlayCount,86400);
            // 获取今日题目访问量
            LambdaQueryWrapper<Practice> practiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
            List<Practice> practiceList = practiceMapper.selectList(practiceLambdaQueryWrapper);
            List<Practice> schoolPracticeList = new ArrayList<>();
            for (Practice practice : practiceList) {
                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
                userLambdaQueryWrapper.eq(User::getId, practice.getTeacherId());
                User user = userMapper.selectOne(userLambdaQueryWrapper);
                if ( user.getSchoolId().equals(schoolId) ) {
                    schoolPracticeList.add(practice);
                }
            }
            int todayPracticeCount = 0;
            for (Practice practice : schoolPracticeList) {
                if ( practice.getVisits() != null )
                    todayPracticeCount += practice.getVisits();
            }
            redisService.set(StatisticPrefix.PRACTICE_VISIT,schoolId + ":" +today,todayPracticeCount,86400);
        }
        // 获取现在所有用户数量
        int userCount = Math.toIntExact(userMapper.selectCount(null));
        redisService.set(StatisticPrefix.USER_COUNT_ADMIN,today,userCount,86400);
        // 获取今日用户活跃度
        LambdaQueryWrapper<User> todayUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        todayUserLambdaQueryWrapper.between(User::getUpdateTime, LocalDateTime.now().minusDays(1), LocalDateTime.now());
        int todayUserCount = Math.toIntExact(userMapper.selectCount(todayUserLambdaQueryWrapper));
        redisService.set(StatisticPrefix.USER_ACTIVE_ADMIN,today,todayUserCount,86400);
        // 获取今日视频总的播放量
        List<Video> videoList = videoMapper.selectList(null);
        int todayVideoPlayCount = 0;
        for (Video video : videoList) {
            if ( video.getPlayCount() != null )
                todayVideoPlayCount += video.getPlayCount();
        }
        redisService.set(StatisticPrefix.VIDEO_PLAYCOUNT_ADMIN,today,todayVideoPlayCount,86400);
        // 获取今日题目访问量
        List<Practice> practiceList = practiceMapper.selectList(null);
        int todayPracticeCount = 0;
        for (Practice practice : practiceList) {
            if ( practice.getVisits() != null )
                todayPracticeCount += practice.getVisits();
        }
        redisService.set(StatisticPrefix.PRACTICE_VISIT_ADMIN,today,todayPracticeCount,86400);
    }

    @Override
    public R importExcelUser(HttpServletRequest request, MultipartFile file, Integer role) throws IOException {
        InputStream stream = new BufferedInputStream(file.getInputStream());
        ExcelListener excelListener = new ExcelListener(userMapper,role);
        ExcelReader excelReader = new ExcelReader(stream,null,excelListener);
        // 读取信息
        excelReader.read(new Sheet(1,1, ExcelUser.class));
        return R.success("导入成功");
    }

    @Override
    public R<List<Suggestion>> getUserSuggestion() {
        return R.success(suggestionMapper.selectList(null));
    }

    @Override
    public R<List<Double>> getTaskDetail(HttpServletRequest request, Integer role) {

        return null;
    }

    @Override
    public R<List<User>> adminGetUserInfo(Integer role) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getRole, role);
        return R.success(userMapper.selectList(lambdaQueryWrapper));
    }
}
